package com.bw.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import com.bw.entity.PhoneUser;
import com.bw.entity.UserInfo;
import com.bw.mapper.UserMapper;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
public class PhoneLoginController {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/phoneLoginPage.do")
    public String phoneLoginPage(){


        return "phoneLogin";

    }

    /**
     * 发送短信验证码
     * @param phone
     */
    @RequestMapping("/sendCode.do")
    @ResponseBody
    public Boolean sendCode(String phone){

        String code = RandomUtil.randomNumbers(5);


        boolean b = ZzyUtils.sendSms(phone, code, 5);

        // 使用Redis数据库保存  短信验证码
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        return b;
    }

    /**
     * 执行登录
     * @param phone
     */
    @RequestMapping("/phoneLogin.do")
    public String phoneLogin(HttpServletRequest request,PhoneUser phone, Model model, HttpServletResponse response){


        // 1. 验证手机号是否存在
        UserInfo userInfo = userMapper.selectByName(phone.getPhone());

        if (userInfo == null){
            model.addAttribute("error","手机号不存在");
            return "phoneLogin";
        }


        // 图片验证码是否正确
        String picCode1 = phone.getPicCode();
        Object picCode = request.getSession().getAttribute("picCode");
        if (!picCode1.equals(picCode)){
            model.addAttribute("error","图片验证码错误");
            return "phoneLogin";
        }
        // 2. 判断短信验证码是否正确
        String code = phone.getPhoneCode();//用户输入的验证码

        Object redisCode = redisTemplate.opsForValue().get(phone.getPhone());

        if (!code.equals(redisCode)){
            model.addAttribute("error","验证码错误");
            return "phoneLogin";
        }

        TokenUtils.responseTokenByCookie(userInfo,response);

        return "success";

    }



    @RequestMapping("/picCode.do")
    public void picCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 自定义纯数字的验证码（随机4位数字，可重复）
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);
// 重新生成code
        lineCaptcha.createCode();
        String code = lineCaptcha.getCode();
        request.getSession().setAttribute("picCode",code);

        lineCaptcha.write(response.getOutputStream());

    }




}
